using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GestaoEventos.Business;
using Framework;

public partial class _Default : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if(!Page.IsPostBack)
		{
			loginForm.Visible = true;
			changePasswordForm.Visible = false;
		}
	}
	protected void loginForm_Authenticate(object sender, AuthenticateEventArgs e)
	{
		if (RegrasGerais.ValidarUsuario(loginForm.UserName, loginForm.Password))
		{
			if (Utility.CriptografarPassword(loginForm.Password) == ConfigurationManager.AppSettings["InitialHashPassword"].ToString())
			{
				Password.Text = loginForm.Password;

				loginForm.Visible = false;
				changePasswordForm.Visible = true;
			}
			else
			{
				Session.Timeout = 30;
				Session["isLogged"] = true;

				Session["IUsuarioLogado"] = RegrasGerais.InterfaceUsuarioLogado(loginForm.UserName, loginForm.Password);

				Response.Redirect("~/Home.aspx");
			}
		}
	}
	protected void changePasswordForm_ChangingPassword(object sender, LoginCancelEventArgs e)
	{
		if(changePasswordForm.NewPassword.Length >= 5)
		{
			if (Password.Text == changePasswordForm.CurrentPassword)
			{
				B_Usuario obj = new B_Usuario();
				obj.IUsuario = RegrasGerais.InterfaceUsuarioLogado(loginForm.UserName, Password.Text);
				obj.IUsuario.Senha = Utility.CriptografarPassword(changePasswordForm.NewPassword);
				obj.Atualizar();

				Session.Timeout = 30;
				Session["isLogged"] = true;

				Session["IUsuarioLogado"] = RegrasGerais.InterfaceUsuarioLogado(loginForm.UserName, changePasswordForm.NewPassword);

				Response.Redirect("~/Home.aspx");
			}
			else
			{
				((Label)changePasswordForm.ChangePasswordTemplateContainer.FindControl("ErroCustom")).Text = "<br />e/ou preencha corretamente o campo 'Senha Atual'";
			}
		}
	}
}
